#include <stdio.h>
#include <fcntl.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h> 

#include <SDL/SDL_thread.h>
#include <SDL/SDL.h>
#include <SDL/SDL_draw.h>//draw头文件需另外添加

#include "stroke.h"

typedef char bool;
#define false 0
#define true 1

#define max(a,b){ a>b?a:b}

SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;



//退出标识
bool quit = false;
//Whether or not to cap the frame rate
bool cap = true;
bool press;

int file_size;

//stroke_t stroke[2];



int screen_w = 800, screen_h = 480;
int show_x, show_y;
int show_w, show_h;

int curX;
int curY;

void draw(int x1,int y1,int x2,int y2) {
	Draw_Line(screen, x1, y1, x2,y2, 0xFF0000);// 画直线
	curX=x2;
	curY=y2;
	SDL_Flip(screen);
}


int
main (int argc, char **argv)
{
  	int count = 0;
  	stroke_t *s[2];
  	s[0] = stroke_alloc (5000);
  	s[1] = stroke_alloc (5000);
  	SDL_Init( SDL_INIT_EVERYTHING );
  	screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
  	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );//填充白色
  	//hello = SDL_LoadBMP( "hello_world.bmp" );
  	//SDL_BlitSurface( hello, NULL, screen, NULL );
  

	SDL_Flip( screen );
  	int num = 0;//打印的时候用，debug
  	int w = 0, h = 0;//打印用，debug


  	while (quit == false)
    	{
      		int mx = 0, my = 0;//接受鼠标移动时的点的坐标
      		SDL_Event event;
      		SDL_WaitEvent (&event);
      		switch (event.type)
		{
			case SDL_QUIT:
	  			quit = true;
	  			break;
			case SDL_KEYDOWN:
	  			break;
			case SDL_MOUSEBUTTONDOWN:
	 			press = true;
	  			SDL_GetMouseState (&mx, &my);
	  			curX=mx;
	  			curY=my;
	  			//stroke_add_point(s[count%2], mx, my);
	  			s[count % 2]->n = 0;
	  			printf ("x=%d;y=%d\n", mx, my);//debug
	  			break;
			case SDL_MOUSEBUTTONUP:
	  			press = false;
	  			SDL_GetMouseState (&mx, &my);
	  			printf ("x=%d;y=%d\n", mx, my);
	  			stroke_finish (s[count % 2]);
	  			//s[count%2]->n = 0;
	  			//count++;
	  			if (count % 2 == 1)
	    			{
	      				double cost = stroke_compare (s[0], s[1], NULL, NULL);
	      				if (cost >= stroke_infinity)
						printf ("匹配失败1！！！\n");
					else{
	      					double score = max (1.0 - 2.5 * cost, 0.0);
	      					if (score > 0.7)
							printf ("匹配成功！！！\n");
	      					else
							printf ("匹配失败2！！！\n");
					}
	      				//sleep (5);
	      			}
	      			/*
	  			for (int i = 0; i < s[count % 2]->n; i++)
	    			{
	      				printf ("p.x=%d;p.y=%d;num=%d;dt=%f;alpha=%f;time=%f\n",
		      				s[0]->p[i].x, s[0]->p[i].y, i, s[0]->p[i].dt,
		      				s[0]->p[i].alpha, s[0]->p[i].t);
	    			}
	    			*/
	  			count++;
	  			break;

			case SDL_MOUSEMOTION:
	  			if (press == true)
	    			{
	      				SDL_GetMouseState (&mx, &my);
	      				draw(curX,curY,mx,my);
	      				stroke_add_point (s[count % 2], mx, my);
	      				printf ("x=%d;y=%d\n", mx, my);
	    			}
	  			break;
			}


    	}


  SDL_FreeSurface(screen);

  SDL_Quit ();
  
  return 0;
}


